# 橘子小说平台 - 开发环境启动脚本

Write-Host "🚀 启动橘子小说平台开发环境..." -ForegroundColor Green

# 检查Node.js是否安装
try {
    $nodeVersion = node --version
    Write-Host "✅ Node.js版本: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ 未找到Node.js，请先安装Node.js" -ForegroundColor Red
    exit 1
}

# 启动前端开发服务器
Write-Host "`n📱 启动前端服务器..." -ForegroundColor Yellow
Start-Process powershell -ArgumentList "-Command", "cd novel-web; npm run dev" -WindowStyle Minimized

# 等待2秒
Start-Sleep -Seconds 2

# 启动后端测试服务器
Write-Host "🔧 启动后端测试服务器..." -ForegroundColor Yellow
Start-Process powershell -ArgumentList "-Command", "cd server; node test-server.js" -WindowStyle Minimized

# 等待3秒让服务器启动
Write-Host "⏳ 等待服务器启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 3

# 检查服务状态
Write-Host "`n📊 检查服务状态..." -ForegroundColor Cyan

# 检查前端端口
$frontendPort = netstat -an | findstr ":5173"
if ($frontendPort) {
    Write-Host "✅ 前端服务器: http://localhost:5173" -ForegroundColor Green
} else {
    Write-Host "❌ 前端服务器未启动" -ForegroundColor Red
}

# 检查后端端口
$backendPort = netstat -an | findstr ":3001"
if ($backendPort) {
    Write-Host "✅ 后端测试服务器: http://localhost:3001" -ForegroundColor Green
} else {
    Write-Host "❌ 后端测试服务器未启动" -ForegroundColor Red
}

# 检查MySQL连接
Write-Host "`n💾 检查数据库连接..." -ForegroundColor Cyan
try {
    cd server
    $dbTest = node test-connection.js 2>&1
    if ($dbTest -like "*连接成功*") {
        Write-Host "✅ 数据库连接正常" -ForegroundColor Green
    } else {
        Write-Host "⚠️  数据库连接失败 - 使用内存存储模式" -ForegroundColor Yellow
        Write-Host "💡 提示: 安装MySQL并启动服务以获得完整功能" -ForegroundColor Cyan
    }
    cd ..
} catch {
    Write-Host "⚠️  无法测试数据库连接" -ForegroundColor Yellow
}

Write-Host "`n🎉 开发环境启动完成!" -ForegroundColor Green
Write-Host "📖 访问地址:" -ForegroundColor Cyan
Write-Host "   前端: http://localhost:5173" -ForegroundColor White
Write-Host "   后端API: http://localhost:3001" -ForegroundColor White
Write-Host "   项目状态: 查看 PROJECT_STATUS_REPORT.md" -ForegroundColor White

Write-Host "`n按任意键退出..." -ForegroundColor Gray
Read-Host 